package com.echat.business.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.echat.business.home.model.OauthUser;
import com.echat.business.home.model.SysUser;
import com.echat.business.system.service.OauthService;
import com.echat.core.constant.CacheKey;
import com.echat.core.constant.ConstantUtils;
import com.echat.core.utils.RestClient;
import com.echat.core.utils.redis.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OauthServiceImpl implements OauthService {


    @Autowired
    private RestClient restClient;


    @Override
    public void getTokenFromUser(SysUser sysUser) {
        //请求oauth2资源服务器 获取令牌
        OauthUser oauthUser = JSONObject.toJavaObject(restClient.postApi(sysUser, JSONObject.class), OauthUser.class);
        if (oauthUser != null) {
            log.info("第三方用户oauth2认证后返回结果:" + oauthUser.toString());
            sysUser.setAccess_token(oauthUser.getAccess_token());
            sysUser.setRefresh_token(oauthUser.getRefresh_token());
            JedisUtil.setObjectValue(CacheKey.USER_KEY + oauthUser.getAccess_token(), sysUser, ConstantUtils.Public.THIRTY_DAYS_SECONDS);
        } else {
            log.error("第三方用户oauth2认证后结果为空");
        }
    }
}
